package com.lewis.jpa.entity;

import java.util.List;

import javax.persistence.Basic;
import javax.persistence.Cacheable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.hibernate.annotations.CollectionId;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;
import org.hibernate.annotations.Type;

@Entity
@Table(name = "USERS")
@Cacheable(true)
// @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
// @NamedQueries({
// @NamedQuery(name = "UserEntity.findAll", query =
// "SELECT u FROM UserEntity u ORDER BY u.name ASC"),
// @NamedQuery(name = "UserEntity.findByNameAndPassword", query =
// "SELECT u FROM UserEntity u WHERE u.name = :name AND u.password = :password"),
// @NamedQuery(name = "UserEntity.findByName", query =
// "SELECT u FROM UserEntity u WHERE u.name = :name")})
@GenericGenerator(name = "UUID_GEN", strategy = "uuid", parameters = { @Parameter(name = "separator", value = "-") })
public class Person extends BaseEntity<Long> {

	@Id
	@TableGenerator(name = "users.id", table = "SEQUENCES", pkColumnName = "NAME", pkColumnValue = "users.id", valueColumnName = "LAST_VALUE")
	@GeneratedValue(strategy = GenerationType.TABLE, generator = "users.id")
	@Basic(optional = false)
	@Column(name = "person_id")
	private Long id;
	@Basic(optional = false)
	@NotNull
	@Size(min = 1, max = 255)
	@Column(name = "FIRSTNAME")
	private String firstname;
	@Basic(optional = false)
	@NotNull
	@Size(min = 1, max = 255)
	@Column(name = "LASTNAME")
	private String lastname;

	@ManyToMany(fetch=FetchType.LAZY)
	@JoinTable(name = "person_course_link", joinColumns = @JoinColumn(name = "person_id"), inverseJoinColumns = @JoinColumn(name = "course_id"))
	@CollectionId(columns = @Column(name = "person_course_link_id"), type = @Type(type = "string"), generator = "UUID_GEN")
	private List<Course> takingCourse;

	@ManyToOne
	@JoinColumn(name = "location_id", nullable = false)
	private Location baseLocation;

	@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER, mappedBy = "owner")
	private List<TeleNumber> numbers;

	public List<Course> getTakingCourse() {
		return takingCourse;
	}

	public void setTakingCourse(List<Course> takingCourse) {
		this.takingCourse = takingCourse;
	}

	public Location getBaseLocation() {
		return baseLocation;
	}

	public void setBaseLocation(Location baseLocation) {
		this.baseLocation = baseLocation;
	}

	public List<TeleNumber> getNumbers() {
		return numbers;
	}

	public void setNumbers(List<TeleNumber> numbers) {
		this.numbers = numbers;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getFirstname() {
		return firstname;
	}

	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}

	public String getLastname() {
		return lastname;
	}

	public void setLastname(String lastname) {
		this.lastname = lastname;
	}

}
